/*
package com.demo.connectdbtest.module.gitlab.service;

import com.demo.connectdbtest.module.gitlab.vo.GitlabConfig;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.gitlab4j.api.GitLabApi;
import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.TreeItem;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@Service
public class GitlabService {

    private final GitlabConfig gitlabConfig;
    private final GitLabApi gitLabApi;

    public GitlabService(final GitlabConfig gitlabConfig, final GitbookConfig gitbookConfig) {
        this.gitlabConfig = gitlabConfig;
        // 传入gitlab服务地址和token，初始化服务
        gitLabApi = new GitLabApi(gitlabConfig.getHost(), gitlabConfig.getAccessToken());
    }

    private void readAllFile(final Project project) {
        // 读取指定项目的，指定分支的工程
        final Project project = gitLabApi.getProjectApi().getProject(gitlabConfig.getNamespace(), gitlabConfig.getProjectName());
        // 获取工程的所有文件
        final List<String> allFil = getAllFiles(project.getId(), gitlabConfig.getDirectory(), gitlabConfig.getBranch());
        // 遍历获取所有文件，进行相关的处理
        allFil.forEach(filePath -> {
            // 读取文件内容
            final InputStream inputStream = gitLabApi.getRepositoryFileApi().getRawFile(projectId, branch, filePath);
            final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            final StringBuilder stringBuilder = new StringBuilder();
            while (reader.ready()) {
                final String lineContent = reader.readLine();
            }
            stringBuilder.append(lineContent);
            // todo 进行相关的处理
        }
    });
}


    // 读取工程仓库的所有文件列表
    @SneakyThrows
    private List<String> getAllFiles(final Integer projectId, final String directory, final String branch) {
        List<String> fileNames = new ArrayList<>();
        gitLabApi.getRepositoryApi().getTree(projectId, directory, branch).forEach(file -> {
            // 如果当前是目录，则继续获取其下的文件列表
            if (file.getType().equals(TreeItem.Type.TREE)) {
                fileNames.addAll(getAllFiles(projectId, file.getPath(), branch));
                return;
            }
            // 如果是文件类型，直接添加
            final String filePath = String.join("/",directory, file.getName());
            fileNames.add(filePath);
            log.info("add file: {}", filePath);
        });
        return fileNames;
    }

    // Webhook回调函数处理，根据自己的需求进行处理
    @SneakyThrows
    public void pushEvent(GitlabPushEvent gitlabPushEvent) {
        log.info("收到Gitlab Webhook请求");
        if (!gitlabPushEvent.getRef().equals(String.format("refs/heads/%s", gitlabConfig.getBranch()))) {
            log.info("非设定分支的push事件，不进行更新");
            return;
        }

        for (GitlabPushEvent.Commit commit: gitlabPushEvent.getCommits()) {
            for (String filePath: commit.getAdded()) {
                // todo 处理新增的文件
            }
            for (String filePath: commit.getModified()) {
                // todo 处理修改的文件
            }
            for (String filePath: commit.getRemoved()) {
                // todo 处理删除的文件
            }
        }
    }
}
*/
